package prbase;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) {
        try {
            Logger.getLogger("").addHandler(new FileHandler("errorlog.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (args.length > 0 && args[0].equals("--install")) {
            FirstInstallDialog dlg = new FirstInstallDialog(null, true);
            dlg.setLocationRelativeTo(null);
            dlg.setVisible(true);
            if (!dlg.getResult().equals("Ok")) {
                return;
            }
        }

        // Properties
        Boolean NeedOptions = false;
        Vars.LoadProperties();
        if (!Vars.prop.containsKey("Username")) {
            Vars.prop.setProperty("Username", "prbase");
            NeedOptions = true;
        }
        if (!Vars.prop.containsKey("Userpass")) {
            Vars.prop.setProperty("Userpass", "prbase");
            NeedOptions = true;
        }
        if (!Vars.prop.containsKey("DB_URL")) {
            Vars.prop.setProperty("DB_URL", "jdbc:mysql://localhost:3306/prbase");
            NeedOptions = true;
        }
        if (!Vars.prop.containsKey("Filestore")) {
            Vars.prop.setProperty("Filestore", ".");
            NeedOptions = true;
        }
        
        DB db = new DB();
        if (db.executeNonQuery("SELECT k FROM projects WHERE k = 0") != 0) {
            NeedOptions = true;
        }

        if (NeedOptions) {
            InstQDialog dlg = new InstQDialog(null, true);
            dlg.setLocationRelativeTo(null);
            dlg.setVisible(true);
            if (dlg.getResult().equals("ExitPressed") || dlg.getResult().equals("False")) {
                return;
            }
            if (dlg.getResult().equals("CreateNew")) {
                FirstInstallDialog dlgcn = new FirstInstallDialog(null, true);
                dlgcn.setLocationRelativeTo(null);
                dlgcn.setVisible(true);
                if (!dlgcn.getResult().equals("Ok")) {
                    return;
                }
            }
            if (dlg.getResult().equals("Connect")) {
                FirstSetupDialog dlgs = new FirstSetupDialog(null, true);
                dlgs.setLocationRelativeTo(null);
                dlgs.setVisible(true);
                if (!dlgs.getResult().equals("Ok")) {
                    return;
                }
            }
        }
        // End of Properties

        MainFrame frm = new MainFrame();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
